package com.zy.openchat.core.model.call;

import android.os.Parcel;
import android.os.Parcelable;

import com.ja.openchat.R;
import com.zy.openchat.App;
import com.zy.openchat.util.DateHelper;

import java.util.Date;

public class LocalCallInfo extends CallBaseInfo implements Parcelable {
    public String rtcToken;
    //是否已取消
    //public boolean isCanceled;
    //呼叫时间
    public long callTime;
    //开始通话时间
    public long startTime;
    //结束通话时间
    public long endTime;
    //通话状态
    public CallManager.CallingState callState;
    //
    public boolean isSendedLocalMsg;
    //来电场景-加入房间后的超时处理
    public long incoming_join_time;

    public String displayDesc() {
        return String.format("[%s]",isVideo ? App.getInstanse().getString(R.string.video_call_text) : App.getInstanse().getString(R.string.voice_call_text));
    }

    public String displayDetailDesc() {
        if (startTime > 0 && endTime > 0) {
            long intervel = endTime - startTime;
            return DateHelper.timeFormattedForCall((int) intervel);
        } else {
            if (callState == CallManager.CallingState.CallingState_Canceled_2_Timeout) {
                return App.getInstanse().getString(R.string.phone_stats_unresponse);
            } else if (callState == CallManager.CallingState.CallingState_C2C_Canceled) {
                return App.getInstanse().getString(R.string.other_cancled_text);
            } else {
                return App.getInstanse().getString(R.string.cancled_text);
            }
        }
    }

    public String displayCallTime() {
        if (startTime > 0) {
            long intervel = new Date().getTime() / 1000 - startTime;
            return DateHelper.timeFormattedForCall((int) intervel);
        }
        return "00:00";
    }

    public static LocalCallInfo callWithRemote(RemoteCallInfo call) {
        LocalCallInfo local = new LocalCallInfo();
        local.callId = call.callId;
        local.channelName = call.channelName;
        local.from = call.from;
        local.to = call.to;
        local.chatId = call.chatId;
        local.isVideo = call.isVideo;
        local.isMeetingAV = call.isMeetingAV;
        local.callState = CallManager.CallingState.CallingState_Incoming_Waiting;
        local.callTime = new Date().getTime() / 1000;
        return local;
    }

    public LocalCallInfo() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.rtcToken);
        dest.writeLong(this.callTime);
        dest.writeLong(this.startTime);
        dest.writeLong(this.endTime);
        dest.writeInt(this.callState == null ? -1 : this.callState.ordinal());
        dest.writeByte(this.isSendedLocalMsg ? (byte) 1 : (byte) 0);
        dest.writeLong(this.incoming_join_time);
    }

    protected LocalCallInfo(Parcel in) {
        super(in);
        this.rtcToken = in.readString();
        this.callTime = in.readLong();
        this.startTime = in.readLong();
        this.endTime = in.readLong();
        int tmpCallState = in.readInt();
        this.callState = tmpCallState == -1 ? null : CallManager.CallingState.values()[tmpCallState];
        this.isSendedLocalMsg = in.readByte() != 0;
        this.incoming_join_time = in.readLong();
    }

    public static final Creator<LocalCallInfo> CREATOR = new Creator<LocalCallInfo>() {
        @Override
        public LocalCallInfo createFromParcel(Parcel source) {
            return new LocalCallInfo(source);
        }

        @Override
        public LocalCallInfo[] newArray(int size) {
            return new LocalCallInfo[size];
        }
    };
}
